<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "entity.CTHD" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Giỏ Hàng</title>
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<jsp:useBean id="model" scope = "session" class = "model.ShoppingCartModel"/>
<%
String sCartAction = (String)request.getParameter("cartAction");
if(sCartAction.equals("addnew")){
	String ma_sp = request.getParameter("masp");
	String ten_sp = request.getParameter("tensp");
	ten_sp = new String(ten_sp.getBytes("8859_1"),"UTF8");
	int gia_sp = Integer.parseInt(request.getParameter("gia"));
	model.themSP(ma_sp, ten_sp, gia_sp, 1);
}
if(sCartAction.equals("delete")){
	model.xoasp(request.getParameter("masp"));
}
if(sCartAction.equals("clear")){
	if (request.getParameter("check")=="1"){
		model.clearItems();
	}
	else{
		model.clearItems();
		response.sendRedirect("saveOrder.jsp");
	}
}
if(sCartAction.equals("update")){
	String itemList[] = request.getParameterValues("masp");
	String slList[] = request.getParameterValues("itemQty");
	if(itemList!=null){
		if(itemList.length == 1){
			int qty = Integer.parseInt(slList[0]);
			model.updatesoluong(itemList[0], qty);
		}
		else{
			for(int i = 0;i<itemList.length;i++){
				int qty = Integer.parseInt(slList[i]);
				model.updatesoluong(itemList[i], qty);
			}
		}
	}
}
%>

<form id="tvgh1" name=frm method = "post">
<div class="login-header">
			<span>Thêm Vào Giỏ Hàng</span>
			<a href="trangchu" class="close">x</a>
			</div>
	<div class="login-content">
	<table id="bang" class="table-bordered">
	<tr>
		<td> Tên sản phẩm </td>
		<td> Giá </td>
		<td>Số lượng </td>
		<td> Giá tổng</td>
		<td> Xóa</td>
	</tr>
	<%
	java.util.List<CTHD> listfuck = new java.util.ArrayList<CTHD>();
	java.util.Enumeration itemsEnum = model.getEnumeration();
	String[] tmpItem;
	int totalcost = model.getCost();
	while(itemsEnum.hasMoreElements()){
		tmpItem = (String[])itemsEnum.nextElement();
		totalcost +=(Integer.parseInt(tmpItem[3])* Integer.parseInt(tmpItem[2]));
		int giachung = Integer.parseInt(tmpItem[3])* Integer.parseInt(tmpItem[2]);
		String bmasp = tmpItem[0];
		String btensp = tmpItem[1];
		String bgia = tmpItem[2];
		String bsoluong = tmpItem[3];
		
		CTHD entity = new CTHD();
		
		entity.setMasp(bmasp);
		entity.setDongia(Integer.parseInt(bgia));
		entity.setSoluong(Integer.parseInt(bsoluong));
		listfuck.add(entity);
	%>
	<tr>
		<td><a href = ctsanpham.jsp?masp=<%=bmasp %>><%=btensp %></a></td>
		<td><%=bgia %></td>
		<td>
			<input id="sl" type =text name = itemQty value ='<%=bsoluong%>'>
		</td>
		<td><%=Integer.toString(giachung) %></td>
		<td><a href = ShoppingCart.jsp?masp=<%=bmasp%>&cartAction=delete>Xoa</a></td>
		<input type = hidden name = masp value = <%=bmasp%>>
	</tr>
	<%
	}
	session.setAttribute("listfuck", listfuck);
	%>
	</table><p>
	
	<input type = button id = 1 value = 'Update san pham' onclick ='doAction(this)'>
	<input type = button id =2 value = 'Xoa gio hang' onclick ='doAction(this)'>
	<input type = button id =3 value = 'Thanh toan' onclick ='doAction(this)'>
	<input type = button id = 4 value = 'Tiep tuc mua hang' onclick ='doAction(this)'>
	</div>
</form>
<script language = javascript>
	function doAction(button){
		if(button.value == "Tiep tuc mua hang"){
			window.open("trangchu","_parent");
		}
		if(button.value =="Update san pham"){
			frm.action = "ShoppingCart.jsp?cartAction=update";
			frm.submit();
			
		}
		if(button.value=="Xoa gio hang"){
			frm.action="ShoppingCart.jsp?cartAction=clear&check=1";
			frm.submit();
			
		}
		//dd
		if(button.value =="Thanh toan"){
			frm.action="confirmOrder";
			frm.submit();
		}
	}
</script>
</table>
</body>
</html>